package com.ynny.project07.dao;

import com.ynny.project07.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDaoImpl implements StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    //根据学生姓名修改
    public  String updateStudent(String name, String newGrade){
        String sql = "UPDATE students SET grade = ? whth name = ?";
        int roet = jdbcTemplate.update(sql,name,newGrade);
        return "修改了" + roet + "条数据";
    }
    @Override
    //根据学号查询
    public Student selectStudent(String number){
        String sql = "select * from students where number = ?";
        List<Student> students = jdbcTemplate.query(
                sql,
                new BeanPropertyRowMapper<>(Student.class),
                number
        );

        System.out.println(students);
        if (students != null && !students.isEmpty()){
            System.out.println(students.get(0));
            return students.get(0);
        }else {
            System.out.println("没有这个学生");
            return null;
        }
    }
    @Override
    //查询所有
    public List<Student> selectAll(){
        String sql = "select * from students";
        List<Student> selectAll = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class) );
        return selectAll;

    }

    @Override
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

    }

}
